package cn.wolfcode.contorller;

import cn.wolfcode.luowowo.domain.Destination;
import cn.wolfcode.luowowo.domain.Region;
import cn.wolfcode.luowowo.query.RegionQuery;
import cn.wolfcode.luowowo.service.IDestinationService;
import cn.wolfcode.luowowo.service.IRegionService;
import cn.wolfcode.luowowo.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.dc.pr.PRError;

import java.util.List;

@Controller
@RequestMapping("/region")
public class RegionController {
    @Autowired
    private IRegionService regionService;
    
    @Autowired
    private IDestinationService destinationService;
    //查询全部列表
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") RegionQuery query){
        Page<Region> page = regionService.query(query);
    
        model.addAttribute("page",page);
        return "region/list";
    }
    //添加与编辑的方法
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Region region){
       
        regionService.saveOrUpdate(region);
        //返回一个json
        return JsonResult.success();
    }
    
    //关联目的地的方法
    @RequestMapping("/getDestByRegionId")
    @ResponseBody
    public Object getDestByRegionId(String rid){
        //将获取的集合返回给前端
        List<Destination> list = destinationService.queryByRegion(rid);
        System.out.println(list);
        return list;
        
    }
    //设置热门方法
    @RequestMapping("/changeHotValue")
    @ResponseBody
    //传了两个值 hot 与id
    public Object changeHotValue(int hot,String id){
        regionService.changeHotValue(id,hot);
        return JsonResult.success();
    }

    //查询约定的目的地
    @RequestMapping("/getDestByDeep")
    @ResponseBody
    public  Object getDestByDeep(int deep){
        //下拉框只需要全部下拉即可 编辑回显后也是需要下拉框显示的是全部数据
        return destinationService.list();
    }
    
    //删除方法
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String id){
        regionService.delete(id);
        return JsonResult.success();
    }
}
